clear all

import delimited using "/fof/dfa/projects/papers/business_cycles/replication_IRF/Table3/dfa_gini_networth_low.csv"

egen row_number = seq(), from(1)

gen gini_2019q4 = .
replace gini_2019q4 = gini if row_number < 123

gen gini_2019q3 = .
replace gini_2019q3 = gini if row_number < 122

gen year_str = substr(date, 1, 4)       // Extract year
gen year = real(year_str)   
gen quarter_str = substr(date, 7, .)    // Extract quarter as string
gen quarter = real(quarter_str) 
gen qtr = yq(year,quarter)	//change from monthly variable to quarterly
format %tq qtr
tsset qtr    

gen diff = 100*(gini-l.gini)
gen diff_2019q4 = 100*(gini_2019q4-l.gini_2019q4)
gen diff_2019q3 = 100*(gini_2019q3-l.gini_2019q3)

tabstat gini gini_2019q4 gini_2019q3 diff diff_2019q4 diff_2019q3, stat(n mean sd skewness kurtosis) save 

tabstat gini_2019q4  diff_2019q4 , stat(n mean sd skewness kurtosis) save 

reg diff_2019q4 l.diff_2019q4 
mat ac=e(b)[1,1] 
mat colnames ac = "autocorr"

putexcel set "/fof/dfa/projects/papers/business_cycles/replication_IRF/Table3/PanelA_gini.xlsx", replace 

putexcel A1= "Sum Test"
putexcel A1:G1, border(bottom) merge hcenter
tabstat gini_2019q4 , stat(n mean sd skewness kurtosis) save 
mat gini_colA = r(StatTotal)[2,1]
mat colnames gini_colA = "GiniMean"
mat rownames gini_colA = "Gini"

tabstat diff_2019q4, stat(n mean sd skewness kurtosis) save 
mat gini_diffs = r(StatTotal)[2...,1]
mat row1 = gini_colA,gini_diffs',ac

putexcel A2= matrix(row1), names nformat(number_d2)


****************** Expansions and Recessions (testing, need to do better here...)

gen expansion = .
*https://fred.stlouisfed.org/series/USRECQM
replace expansion = 0 if (year==2007 & qtr==4) | (year==2008) | (year==2009 & qtr<=2) | (year==2001) | (year==1990 & qtr>=3) | (year==1991 & qtr<=1) | (year==2019 & qtr==4) | (year==2020 & qtr==1)
replace expansion = 1 if missing(expansion)==1

    gen gini_2019q4_rec = .
    replace gini_2019q4_rec = gini_2019q4 if expansion == 0
    gen gini_2019q4_exp = .
    replace gini_2019q4_exp = gini_2019q4 if expansion == 1

    gen diff_2019q4_rec = .
    replace diff_2019q4_rec = diff_2019q4 if expansion == 0
    gen diff_2019q4_exp = .
    replace diff_2019q4_exp = diff_2019q4 if expansion == 1
    
    
reg diff_2019q4_rec l.diff_2019q4_rec 
mat ac=e(b)[1,1] 
mat colnames ac = "autocorr"

putexcel I1= "Recession"
putexcel I1:O1, border(bottom) merge hcenter
tabstat gini_2019q4_rec , stat(n mean sd skewness kurtosis) save 
mat gini_colA = r(StatTotal)[2,1]

tabstat diff_2019q4_rec, stat(n mean sd skewness kurtosis) save 
mat gini_diffs = r(StatTotal)[2...,1]
mat row1 = gini_colA,gini_diffs',ac

putexcel I2= matrix(row1), names nformat(number_d2)


reg diff_2019q4_exp l.diff_2019q4_exp 
mat ac=e(b)[1,1] 
mat colnames ac = "autocorr"

putexcel P1= "Expansion"
putexcel P1:V1, border(bottom) merge hcenter
tabstat gini_2019q4_exp , stat(n mean sd skewness kurtosis) save 
mat gini_colA = r(StatTotal)[2,1]

tabstat diff_2019q4_exp, stat(n mean sd skewness kurtosis) save 
mat gini_diffs = r(StatTotal)[2...,1]
mat row1 = gini_colA,gini_diffs',ac

putexcel P2= matrix(row1), names nformat(number_d2)

*********************** ANNUAL *************************************************
preserve
keep if quarter_str=="4"
tsset year

gen ann_diff = 100*(gini-l.gini)
gen ann_diff_2019q4 = 100*(gini_2019q4-l.gini_2019q4)
gen ann_diff_2019q3 = 100*(gini_2019q3-l.gini_2019q3)

tabstat gini gini_2019q4 gini_2019q3 ann_diff ann_diff_2019q4 ann_diff_2019q3, stat(n mean sd skewness kurtosis) save 

tabstat gini_2019q4  ann_diff_2019q4 , stat(n mean sd skewness kurtosis) save 

reg ann_diff_2019q4 l.ann_diff_2019q4 
mat ac=e(b)[1,1] 
mat colnames ac = "autocorr"


putexcel W1= "Annual"
putexcel W1:AC1, border(bottom) merge hcenter
tabstat gini_2019q4 , stat(n mean sd skewness kurtosis) save 
mat gini_colA = r(StatTotal)[2,1]
mat colnames gini_colA = "GiniMean"
mat rownames gini_colA = "Gini"

tabstat ann_diff_2019q4, stat(n mean sd skewness kurtosis) save 
mat gini_diffs = r(StatTotal)[2...,1]
mat row1 = gini_colA,gini_diffs',ac

putexcel W2= matrix(row1), names nformat(number_d2)


